package org.mixed.script.modules.db.table;

import org.apache.ibatis.mapping.BoundSql;
import org.mixed.script.modules.db.DBModule;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Table {

    private final String tableName;
    private final DBModule dbModule;
    private final List<String> columns = new ArrayList<>();
    private final List<String> groups = new ArrayList<>();
    private final List<String> orders = new ArrayList<>();

    public Table(String tableName, DBModule dbModule) {
        this.tableName = tableName;
        this.dbModule = dbModule;
    }

    Where where = new Where(this);

    public List<Map<String, Object>> select() {
        return dbModule.select();
    }

    public Map<String, Object> selectOne() {
        return dbModule.selectOne();
    }

    private BoundSql buildSelect() {
        StringBuilder builder = new StringBuilder();
        builder.append("select ");
        if (columns.isEmpty()) {
            builder.append("*");
        } else {
            builder.append(String.join(",", columns));
        }
        builder.append(" from ").append(tableName);
        builder.append(where.getSql());
        if (!groups.isEmpty()) {
            builder.append(" group by ");
            builder.append(String.join(",", groups));
        }
        if (!orders.isEmpty()) {
            builder.append(" order by ");
            builder.append(String.join(",", orders));
        }
//        BoundSql boundSql = new BoundSql(runtimeContext, builder.toString(), where.getParams(), sqlModule);
//        boundSql.setExcludeColumns(excludeColumns);
//        return boundSql;
        return null;
    }
}
